package com.itwxz.forum.controller.pc;

import cn.hutool.crypto.digest.BCrypt;
import com.itwxz.common.annotation.Log;
import com.itwxz.common.core.controller.BaseController;
import com.itwxz.common.core.domain.AjaxResult;
import com.itwxz.common.core.page.TableDataInfo;
import com.itwxz.common.enums.BusinessType;
import com.itwxz.common.utils.StringUtils;
import com.itwxz.common.utils.poi.ExcelUtil;
import com.itwxz.forum.domain.po.ForumUser;
import com.itwxz.forum.service.ForumUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 论坛用户信息Controller
 *
 * @author gastronome
 * @date 2025-05-04
 */
@Tag(name = "论坛用户信息Controller")
@Slf4j
@RestController
@RequestMapping("/forum/user")
public class ForumUserController extends BaseController {
	@Autowired
	private ForumUserService forumUserService;

	/**
	 * 查询论坛用户信息列表
	 */
	@Operation(summary = "查询论坛用户信息列表")
	@PreAuthorize("@ss.hasPermi('forum:user:list')")
	@GetMapping("/list")
	public TableDataInfo list(ForumUser forumUser) {
		startPage();
		List<ForumUser> list = forumUserService.selectForumUserList(forumUser);
		return getDataTable(list);
	}

	/**
	 * 导出论坛用户信息列表
	 */
	@Operation(summary = "导出论坛用户信息列表")
	@PreAuthorize("@ss.hasPermi('forum:user:export')")
	@Log(title = "论坛用户信息", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, ForumUser forumUser) {
		List<ForumUser> list = forumUserService.selectForumUserList(forumUser);
		ExcelUtil<ForumUser> util = new ExcelUtil<>(ForumUser.class);
		util.exportExcel(response, list, "论坛用户信息数据");
	}

	/**
	 * 获取论坛用户信息详细信息
	 */
	@Operation(summary = "获取论坛用户信息详细信息")
	@PreAuthorize("@ss.hasPermi('forum:user:query')")
	@GetMapping(value = "/{userId}")
	public AjaxResult getInfo(@PathVariable("userId") Long userId) {
		return success(forumUserService.getById(userId));
	}

	/**
	 * 新增论坛用户信息
	 */
	@Operation(summary = "新增论坛用户信息")
	@PreAuthorize("@ss.hasPermi('forum:user:add')")
	@Log(title = "论坛用户信息", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody ForumUser forumUser) {
		return toAjax(forumUserService.save(forumUser));
	}

	/**
	 * 修改论坛用户信息
	 */
	@Operation(summary = "修改论坛用户信息")
	@PreAuthorize("@ss.hasPermi('forum:user:edit')")
	@Log(title = "论坛用户信息", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody ForumUser forumUser) {
		return toAjax(forumUserService.updateById(forumUser));
	}

	/**
	 * 修改论坛用户密码
	 */
	@Operation(summary = "修改论坛用户密码")
	@PreAuthorize("@ss.hasPermi('forum:user:edit')")
	@Log(title = "论坛用户密码", businessType = BusinessType.UPDATE)
	@PutMapping("/password")
	public AjaxResult updatePassword(@RequestBody ForumUser forumUser) {
		// 参数校验
		if (forumUser.getUserId() == null || StringUtils.isBlank(forumUser.getPassword()))
			return AjaxResult.error("用户ID和新密码不能为空");

		ForumUser user = new ForumUser();
		user.setUserId(forumUser.getUserId());
		user.setPassword((BCrypt.hashpw(forumUser.getPassword(), BCrypt.gensalt())));
		return toAjax(forumUserService.updateById(user));
	}

	/**
	 * 删除论坛用户信息
	 */
	@Operation(summary = "删除论坛用户信息")
	@PreAuthorize("@ss.hasPermi('forum:user:remove')")
	@Log(title = "论坛用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
	public AjaxResult remove(@PathVariable Long[] userIds) {
		return toAjax(forumUserService.removeByIds(Arrays.asList(userIds)));
	}
}
